using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;
using NCS.Library;
using NCS.Library.Components.Partner;
using NCS.Library.Components.Security;
using NCS.Library.Utilities;

namespace Portal.Administrator.Partner
{
    public partial class PartnerManagement : AuthenticatedControl
	{
		protected void Page_Load(object sender, EventArgs e)
		{
            txtAvatar.fpUploadDir = Definition.UploadPath;
		}
		protected override void OnPreRender(EventArgs e)
		{
			base.OnPreRender(e);
            DataTable dtPartners = PartnerController.GetPartners();
            dtgPartners.DataSource = dtPartners;
            dtgPartners.DataBind();
           
		}
        protected void dtgPartners_RowCommand(object sender, GridViewCommandEventArgs e)
		{
			int partnerId;
			switch (e.CommandName)
			{
				case "editpartner":
                    partnerId = ConvertUtility.ToInt32(e.CommandArgument);
                    PartnerInfo partner = PartnerController.GetPartner(partnerId);
					txtPartnerID.Text = partner.PartnerID.ToString();
                    txtPartnerName.Text = partner.PartnerName;
                    txtPartnerDescription.Text = partner.PartnerDescription;
                    txtAddress.Text = partner.Address;
                    txtPhone.Text = partner.Phone;
                    txtFax.Text = partner.Fax;
                    txtEmail.Text = partner.Email;
                    txtUserDD.Text = partner.UserDD;
                    txtAvatar.Text = partner.Avatar;
					break;
				case "deletepartner":
					try
					{
						partnerId = ConvertUtility.ToInt32(e.CommandArgument);
						PartnerController.DeletePartner(partnerId);
                        lblUpdate.Text = MiscUtility.MSG_UPDATE_SUCCESS;
                        this.AddLog("Partner", " try delete partner " + e.CommandArgument + " success");
					}
					catch (Exception ex)
					{
						Response.Write(ex.Message);
                        this.AddLog("Partner", " try delete partner " + e.CommandArgument + " error: " + ex.Message);
					}
					break;
				default:
					break;
			}
		}
        protected void dtgPartners_RowDataBound(object sender, GridViewRowEventArgs e)
		{
			if (e.Row.RowType == DataControlRowType.DataRow)
			{
                NCS.WebControls.ButtonRoleDelete btnDeletePartner = (NCS.WebControls.ButtonRoleDelete)e.Row.FindControl("btnDeletePartner");
                btnDeletePartner.Attributes.Add("onclick", MiscUtility.MSG_DELETE_CONFIRM);
			}
		}
		protected void btnUpdate_Click(object sender, EventArgs e)
		{
			try
			{
                int partnerId = ConvertUtility.ToInt32(txtPartnerID.Text);
			    
                PartnerInfo partner = PartnerController.GetPartner(partnerId);
			    
                partner.PartnerName = txtPartnerName.Text;
                partner.PartnerDescription = txtPartnerDescription.Text;
                partner.Address = txtAddress.Text;
                partner.Phone = txtPhone.Text;
                partner.Fax = txtFax.Text;
                partner.Email = txtEmail.Text;
                partner.UserDD = txtUserDD.Text;
                partner.Avatar = txtAvatar.Text;
			    
                PartnerController.UpdatePartner(partner);
                lblUpdate.Text = MiscUtility.MSG_UPDATE_SUCCESS;
                this.AddLog("Partner", " try update partner " + txtPartnerName.Text + " success");
			}
			catch (Exception ex)
			{
				Response.Write(ex.Message);
                this.AddLog("Partner", " try update partner " + txtPartnerName.Text + " error: " + ex.Message);
			}
            btnCancel_Click(null, null);
		}
		protected void btnAdd_Click(object sender, EventArgs e)
		{
			try
			{
                PartnerInfo partner = new PartnerInfo();
			    
                partner.PartnerName = txtPartnerName.Text;
                partner.PartnerDescription = txtPartnerDescription.Text;
                partner.Address = txtAddress.Text;
                partner.Phone = txtPhone.Text;
                partner.Fax = txtFax.Text;
                partner.Email = txtEmail.Text;
                partner.UserDD = txtUserDD.Text;
                partner.Avatar = txtAvatar.Text;
			    
                PartnerController.AddPartner(partner);
                lblUpdate.Text = MiscUtility.MSG_UPDATE_SUCCESS;
                this.AddLog("Partner", " try add partner " + txtPartnerName.Text + " success");
			}
			catch (Exception ex)
			{
				Response.Write(ex.Message);
                this.AddLog("Partner", " try add partner " + txtPartnerName.Text + " error: "+ ex.Message);
			}
            btnCancel_Click(null, null);
		}
		protected void btnDelete_Click(object sender, EventArgs e)
		{
			try
			{
                int partnerId = ConvertUtility.ToInt32(txtPartnerID.Text);
                PartnerController.DeletePartner(partnerId);
                lblUpdate.Text = MiscUtility.MSG_UPDATE_SUCCESS;
                this.AddLog("Partner", " try delete partner " + txtPartnerName.Text + " success");
			}
			catch (Exception ex)
			{
				Response.Write(ex.Message);
                this.AddLog("Partner", " try delete partner " + txtPartnerName.Text + " error: "+ ex.Message);
			}
            btnCancel_Click(null, null);
		}

        protected void btnCancel_Click(object sender, EventArgs e)
        {
            txtPartnerName.Text = "";
            txtPartnerDescription.Text = "";
            txtAddress.Text = "";
            txtPhone.Text = "";
            txtFax.Text = "";
            txtEmail.Text = "";
            txtUserDD.Text = "";
            txtAvatar.Text = "";
        }
	}
}